package com.huluobo.a2307monthdemo2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.huluobo.a2307monthdemo2.adapter.CommonPagerAdapter
import com.huluobo.a2307monthdemo2.databinding.ActivityMainBinding
import com.huluobo.a2307monthdemo2.home.HomeFragment
import com.huluobo.a2307monthdemo2.message.MessageFragment
import com.huluobo.a2307monthdemo2.mine.MineFragment
import com.huluobo.a2307monthdemo2.start_liev.StartLiveActivity
import com.huluobo.a2307monthdemo2.video.VideoFragment
import com.huluobo.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
    private val fragments = listOf(
        HomeFragment(),
        VideoFragment(),
        MessageFragment(),
        MineFragment()
    )

    override fun initData() {
        binding.mainVp2.adapter = CommonPagerAdapter(this, fragments)
    }

    override fun initView() {
        binding.mainVp2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> binding.mainNav.menu.getItem(0).isChecked = true
                    1 -> binding.mainNav.menu.getItem(1).isChecked = true
                    2 -> binding.mainNav.menu.getItem(3).isChecked = true
                    3 -> binding.mainNav.menu.getItem(4).isChecked = true
                }
            }
        })

        binding.mainNav.setOnItemSelectedListener {
            when (it.itemId) {
                R.id.main_tab_home -> binding.mainVp2.currentItem = 0
                R.id.main_tab_videos -> binding.mainVp2.currentItem = 1
                R.id.main_tab_start_live -> startAct(StartLiveActivity::class.java)
                R.id.main_tab_message -> binding.mainVp2.currentItem = 2
                R.id.main_tab_mine -> binding.mainVp2.currentItem = 3
            }
            true
        }
    }

    override fun onResume() {
        super.onResume()
        binding.mainVp2.currentItem = 1
        binding.mainNav.menu.getItem(1).isChecked = true
    }
}